package com.bbs.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.bbs.bean.BbsTitleInfo;
import com.bbs.bean.BbsUserInfo;
import com.bbs.services.BbsTitleInfoManager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BbsTitleInfoAction extends ActionSupport implements Preparable,ModelDriven<BbsTitleInfo>{
	private static final String  LIST="/bbsTitleInfo/bbsTitleInfoList.jsp";
	private static final String  EDIT="/bbsTitleInfo/bbsTitleInfoEdit.jsp";
	private static final String  ADD="/bbsTitleInfo/bbsTitleInfoAdd.jsp";
	private static final String  SHOW="/bbsTitleInfo/bbsTitleInfoShow.jsp";
	private static final String  MAINTITLEINFO="/bbsTitleInfo/bbsTitleMainShow.jsp";
	
	private BbsTitleInfo bbsTitleInfo;
	private BbsTitleInfoManager bbsTitleInfoManager;
	private int id;
	private List<BbsTitleInfo> bbsTitleInfoList;
	private int page;
	private int maxpage;
	private int blockid; 
	
	
	public int getBlockid() {
		return blockid;
	}

	public void setBlockid(int blockid) {
		this.blockid = blockid;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(int maxpage) {
		this.maxpage = maxpage;
	}

	public BbsTitleInfo getBbsTitleInfo() {
		return bbsTitleInfo;
	}

	public void setBbsTitleInfo(BbsTitleInfo bbsTitleInfo) {
		this.bbsTitleInfo = bbsTitleInfo;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<BbsTitleInfo> getBbsTitleInfoList() {
		return bbsTitleInfoList;
	}

	public void setBbsTitleInfoList(List<BbsTitleInfo> bbsTitleInfoList) {
		this.bbsTitleInfoList = bbsTitleInfoList;
	}

	public void setBbsTitleInfoManager(BbsTitleInfoManager bbsTitleInfoManager) {
		this.bbsTitleInfoManager = bbsTitleInfoManager;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		if(bbsTitleInfo==null){
			bbsTitleInfo=new BbsTitleInfo();
		}
	}

	public BbsTitleInfo getModel() {
		// TODO Auto-generated method stub
		return bbsTitleInfo;
	}	
	
	public String list(){
		bbsTitleInfoList=bbsTitleInfoManager.findAll();
		return LIST;
	}
	
	public String addShow(){
		
		return ADD;
	}
	
	public String edit(){
		bbsTitleInfo=bbsTitleInfoManager.findId(id);
		return EDIT;
	}
	
	public String updateInfo(){
		bbsTitleInfoManager.update(bbsTitleInfo);
		bbsTitleInfoList=bbsTitleInfoManager.findAll();
		
		return LIST;
	}
	
	public String addInfo(){
		BbsUserInfo uer=(BbsUserInfo) ServletActionContext.getRequest().getSession().getAttribute("bbsUserInfo");
		bbsTitleInfo.setTitleUserid(uer.getUserid());
		bbsTitleInfo.setTitleUsername(uer.getUsername());
		bbsTitleInfo.setTitleBlockid(bbsTitleInfo.getTitleBlockid());
		bbsTitleInfoManager.save(bbsTitleInfo);
		if(page==0){
			page=1;
		}
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountByBlockId(bbsTitleInfo.getTitleBlockid())+1;
		bbsTitleInfoList=bbsTitleInfoManager.findByBlock(bbsTitleInfo.getTitleBlockid(),page);
		maxpage=maxpage/20+1;
		return  MAINTITLEINFO;
	}
	
	public String show(){
		bbsTitleInfo=bbsTitleInfoManager.findId(id);
		return SHOW;
	}
	
	public String showmainTitleInfo(){
		if(page==0){
			page=1;
		}
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountInfo()+1;
		bbsTitleInfoList=bbsTitleInfoManager.findAllByPage(countPage);
		maxpage=maxpage/20+1;
		return  MAINTITLEINFO;
	}
	
	public String prePage(){
		page=page-1;
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountInfo()+1;
		bbsTitleInfoList=bbsTitleInfoManager.findAllByPage(countPage);
		maxpage=maxpage/20+1;
		return MAINTITLEINFO;
	}
	
	public String nextPage(){
		page=page+1;
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountInfo()+1;
		bbsTitleInfoList=bbsTitleInfoManager.findAllByPage(countPage);
		maxpage=maxpage/20+1;
		return MAINTITLEINFO;
	}
	
	
	public String findByblock(){
		if(page==0){
			page=1;
		}
		int countPage=page*20-20;
		maxpage=bbsTitleInfoManager.getCountByBlockId(bbsTitleInfo.getTitleBlockid())+1;
		bbsTitleInfoList=bbsTitleInfoManager.findByBlock(bbsTitleInfo.getTitleBlockid(),page);
		maxpage=maxpage/20+1;
		return  MAINTITLEINFO;
		
	}
	
}
